#!/usr/bin/env python

import numpy as np
import pylab as pl
import os.path as osp
import sys
import matplotlib
from matplotlib.font_manager import FontProperties as FP

# Fixes font size
fp = FP()
fp.set_size('small')

matplotlib.rc('text', usetex=True) # Use latex formatting!

ptotal=1400 # total packet size [bytes] 
o_udp=48 # overhead from UDP header in [bytes] ipv6
q=[1/8,1,2] # data size for a entry in the coding vector [bytes]

f_q0=[] # function value
f_q1=[] # function value
f_q2=[] # function value

 #(x * 0.1 for x in range(0, 10))

for G in range(0,500): # For generation sizes 0...1000
	value=-q[0]*G**2+(ptotal-o_udp)*G
	f_q0.append(value/1024)

for G in range(0,500): # For generation sizes 0...1000
	value=-q[1]*G**2+(ptotal-o_udp)*G
	f_q1.append(value/1024)

for G in range(0,500): # For generation sizes 0...1000
	value=-q[2]*G**2+(ptotal-o_udp)*G
	f_q2.append(value/1024)

pl.plot(f_q0)
pl.plot(f_q1)
pl.plot(f_q2)
pl.xlabel('Generation size [-]')
pl.ylabel('Source data in a generation [kbytes]')
pl.legend(['GF(2) Packet size '+str(ptotal)+' bytes','GF($2^{8}$) Packet size '+str(ptotal)+' bytes','GF($2^{16}$) Packet size '+str(ptotal)+' bytes'],loc='lower right',prop=fp)
pl.grid('on')

#pl.show()

svndir = osp.abspath('../../rapport/figs')
pl.savefig(svndir+'/'+'gensize_vs_gopsize'+'.eps')
















#def new_frame(filep):
#	line = '1'
#	while not line.startswith('[/FRAME]'):
#		line = filep.readline()
#		if line.startswith('pkt_pos='):
#			pos = int(line[line.find('=')+1:])
#		if line.startswith('pict_type='):
#			pict_type = line[line.find('=')+1:-1]
#	return pos, pict_type


#if __name__ == '__main__':
#	input_filename = sys.argv[1]

#	f = open(input_filename,'r')

#	pos_type = []

#	##### DATA EXTRACTION #####

#	line = '1'
#	while line:
#		line = f.readline()
#		if line.startswith('[FRAME]'):
#			position, pict_type = new_frame(f)
#			pos_type.append((position, pict_type))

#	# Determine distribution of |Gop20|

#	GOP_size_list=[]
#	GOP_20_temp_size=0
#	i_ref=0

#	for i in range(len(pos_type)-1)	:
#	
#		if (str(pos_type[i+1][1]).lower()=='i'):
#			GOP_20_temp_size=pos_type[i][0]-pos_type[i_ref][0]
#			GOP_size_list.append(GOP_20_temp_size)
#			i_ref=i

#	for i in range(len(GOP_size_list)): 	
#		GOP_size_list[i]=GOP_size_list[i]/1024 # Scale to kbytes !!
#	
#	##### PLOTTINGS #######

#	histogram_bin_size = 200
#	max_size=1000
#	min_size=0

#	hist_GOP20_size = pl.histogram(GOP_size_list, histogram_bin_size, (min_size, max_size))

#	fp = FP()
#	fp.set_size('small')

#	fig = pl.figure()
#	ax1 = fig.add_subplot(111)
#	bar_p = ax1.bar(hist_GOP20_size[1][:-1],hist_GOP20_size[0], width=(max_size-min_size)/histogram_bin_size)

#	ax1.set_xlabel('|GOP20| [kBytes]')
#	ax1.set_ylabel('GOP count [-]')
#	ax1.grid('on')

##	pl.show()

#	svndir = osp.abspath('../../rapport/figs')
#	pl.savefig(svndir+'/'+'dist_GOP20_size'+'.eps')






